package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.NewsCommentMapper;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsPageMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsPageServiceImpl implements INewsPageService {
    @Autowired
    private NewsPageMapper newsPageMapper;
    @Autowired
    private NewsContentMapper newsContentMapper;
    @Autowired
    private NewsCommentMapper newsCommentMapper;

    @Override
    public void saveOrUpdate(NewsPage entity) {
        entity.setAuthor("admin");
        entity.setCreateTime(new Date());
        NewsContent content = entity.getNewsContent();
        if (entity.getId() == null) {
            newsPageMapper.insert(entity);
            content.setId(entity.getId());
            newsContentMapper.insert(content);

        } else {
            newsPageMapper.updateByPrimaryKey(entity);
            content.setId(entity.getId());
            newsContentMapper.updateByPrimaryKey(content);
        }
    }

    @Override
    public PageInfo<NewsPage> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<NewsPage> list = newsPageMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public String getContentById(Long id) {
        return newsContentMapper.selectByPrimaryKey(id).getContent();
    }

    @Override
    public NewsPage selectContent(Long id) {
        NewsPage newsPage = newsPageMapper.selectByPrimaryKey(id);
        NewsContent newsContent = newsContentMapper.selectByPrimaryKey(id);
        newsPage.setNewsContent(newsContent);
        return newsPage;
    }


    @Override
    public List<NewsPage> selectRecommendNews(Long id) {
        return newsPageMapper.selectRecommendNews(id);
    }

}
